package sandan.gyl.login.action;

import java.util.Collection;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sandan.gyl.base.action.BaseAction;
import sandan.gyl.domain.basedata.User;
import sandan.gyl.domain.privilege.Privilege;
import sandan.gyl.login.service.LoginService;
import sandan.gyl.privilege.service.PrivilegeService;

@Controller("loginAction")
@Scope("prototype")
public class LoginAction extends BaseAction<User> {
	@Resource(name = "loginService")
	private LoginService loginService;

	@Resource(name = "privilegeService")
	private PrivilegeService privilegeService;

	public String authentication() {
		User user = this.loginService.authentication(this.getModel()
				.getUsername(), this.getModel().getPassword());
		if (user == null) {
			this.addActionError("用户名或者密码错误");
			return "login";
		} else {
			Collection<Privilege> functions = this.privilegeService
					.getFunctionTreeByUid(user.getUid());
			this.getSession().setAttribute("user", user);// 把该用户能够访问到的功能权限放入到session中
			this.getSession().setAttribute("functions", functions);
			return "index";
		}

	}
}
